<!DOCTYPE html>
<html>
  
<head>
  <meta charset="utf-8">
  <meta name="author" content="饿包子" />
  
  
  <title>正则表达式的入门教学 | 饿包子博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="前端入门," />
  

  
  <meta name="description" content="正则表达式，偶尔又需要，平时不需要，每次都需要去查资料，还有多找几遍，还是自己整理一下吧，知道没接触的人最需要知道哪些">

  
  
    <link rel="icon" href="/blog/images/xian.ico">
    <link rel="apple-touch-icon" href="/blog/images/Logo.png">
  

  
<link rel="stylesheet" href="/blog/css/Awesome.min.css">


  
<link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">


  
  
<script src="/blog/common/jquery.min.js"></script>

  
    
<script src="/blog/common/Av.min.js"></script>

  

  
    
<script src="/blog/common/MathJax.js"></script>

  

  
    
<script src="/blog/common/Valine.min.js"></script>

  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"gE1EosDsVgB2slV4E92yPkcG-gzGzoHsz","appkey":"niR787UVTMyJQrR6gmOiJCKe","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2018-06-10",
    passwords: ["a621ab606db2a11f63edc576a729843b8269250dc324206871d90635ac5e531c", ],
    is_post: true,
    lock: false,
    author: "饿包子",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":false},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  
<script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">清风逐月</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 我思故我在</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">主页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">日记</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/lixianbin1/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            主页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">
            日记
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2020-11-09
    </span>
    
      <span>
        | <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E5%85%A5%E9%97%A8/"><i class="fa fa-bookmark"></i>前端入门</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
    <span>
       | <i class="fa fa-calendar"></i>更新时间:2020-11-9 15:43 
    </span>
  </div>
  <h1 class="passage-title">
    正则表达式的入门教学
  </h1>
  
  <article class="passage-article">
    <h2 id="RegExp对象"><a href="#RegExp对象" class="headerlink" title="RegExp对象"></a>RegExp对象</h2><p>使用正则匹配需要正则表达式，就是RegExp对象 </p>
<blockquote>
<p>语法：/主体/修饰符(可选)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">let reg = new RegExp(&quot;abc&quot;)</span><br><span class="line"></span><br><span class="line">等同于</span><br><span class="line"></span><br><span class="line">let reg = /abc/</span><br></pre></td></tr></table></figure>

<h2 id="匹配方法"><a href="#匹配方法" class="headerlink" title="匹配方法"></a>匹配方法</h2><p>当有了匹配公式后，还需要使用JavaScript的匹配方法进行字符串的匹配。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 查询匹配公式的字符串位置</span></span><br><span class="line">/a/.<span class="title function_">exec</span>(<span class="string">&#x27;abc&#x27;</span>)             <span class="comment">// [&quot;a&quot;, index: 0, input: &quot;abc&quot;, groups: undefined]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 查询字符串是否匹配正则公式</span></span><br><span class="line">/a/.<span class="title function_">test</span>(<span class="string">&#x27;abc&#x27;</span>)             <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取匹配公式的第一个字符串索引</span></span><br><span class="line"><span class="string">&#x27;abc&#x27;</span>.<span class="title function_">search</span>(<span class="regexp">/a/</span>)           <span class="comment">// 1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 查询字符串中有多少匹配的字符串</span></span><br><span class="line"><span class="string">&#x27;abc&#x27;</span>.<span class="title function_">match</span>(<span class="regexp">/a/</span>).<span class="property">length</span>      <span class="comment">// 1 </span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 匹配字符串后替换成指定字符串</span></span><br><span class="line"><span class="string">&#x27;abc&#x27;</span>.<span class="title function_">replace</span>(<span class="regexp">/b/</span>,<span class="string">&#x27;a&#x27;</span>)       <span class="comment">// &#x27;aac&#x27;</span></span><br></pre></td></tr></table></figure>

<h2 id="修饰符"><a href="#修饰符" class="headerlink" title="修饰符"></a>修饰符</h2><p>i:忽略大小写 g:全局匹配 m:执行多行匹配 s:特殊字符圆点 . 中包含换行符 \n</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x27;abccba&#x27;.match(/a/g)   // [a,a]</span><br></pre></td></tr></table></figure>

<h2 id="公式规则"><a href="#公式规则" class="headerlink" title="公式规则"></a>公式规则</h2><h3 id="元字符"><a href="#元字符" class="headerlink" title="元字符"></a>元字符</h3><p><code>\d</code> 查找数字<br><code>\D</code> 查找非数字<br><code>\s</code> 查找空格<br><code>\S</code> 查找非空格<br><code>\w</code> 查找单词<br><code>\W</code> 查找非单词<br><code>\n</code> 匹配换行符<br><code>\r</code> 匹配回车符</p>
<p><code>\b</code> 匹配单词的边<br><code>\B</code> 匹配单词的里</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&quot;Hello number beact&quot;.match(/\bbe/) /*查找在单词边上的be*/</span><br><span class="line">&quot;Hello number beact&quot;.match(/\Bbe/) /*查找不在边上的be*/</span><br></pre></td></tr></table></figure>

<p><code>^x</code> 匹配开头是x的字符串<br><code>x$</code> 匹配结尾是x的字符串</p>
<h3 id="字符镞"><a href="#字符镞" class="headerlink" title="字符镞"></a>字符镞</h3><p><code>[a-z]</code> //匹配所有的小写字母<br><code>[A-Z]</code> //匹配所有的大写字母<br><code>[a-zA-Z]</code> //匹配所有的字母<br><code>[0-9]</code> //匹配所有的数字<br><code>[0-9\.\-]</code> //匹配所有的数字，句号和减号<br><code>[ \f\r\t\n]</code> //匹配所有的空白字符<br><code>[abcd]</code> //匹配集合内的任意字符<br><code>[^abcd]</code> //匹配集合外的任意字符</p>
<h3 id="量词"><a href="#量词" class="headerlink" title="量词"></a>量词</h3><p><code>n+</code> //匹配的字符串至少包含一个n<br><code>n*</code> //匹配的字符串可以有零个或多个n<br><code>n?</code> //匹配的字符串可以有零个或多个n<br>当此字符紧随任何其他限定符之后时，匹配模式是”非贪心的”,”o+?”只匹配单个”o”，而”o+”匹配所有”o”</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&quot;AASDASD&quot;.match(/A+/g) /*[&quot;AA&quot;, &quot;A&quot;] */</span><br><span class="line">&quot;AASDASD&quot;.match(/A+?/g)/*[&quot;A&quot;, &quot;A&quot;, &quot;A&quot;]*/</span><br><span class="line">&quot;AASDASD&quot;.match(/A*/g) /*[&quot;AA&quot;, &quot;&quot;, &quot;&quot;, &quot;A&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;]*/</span><br></pre></td></tr></table></figure>
<p><code>x&#123;n&#125;</code> n是非负整数。正好匹配 n 次<br><code>x&#123;n,&#125;</code>n是非负整数。至少匹配 n 次<br><code>x&#123;n,m&#125;</code>n和m是非负整数，其中 n &lt;= m。匹配至少 n 次，至多 m 次</p>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#RegExp%E5%AF%B9%E8%B1%A1"><span class="toc-text">RegExp对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8C%B9%E9%85%8D%E6%96%B9%E6%B3%95"><span class="toc-text">匹配方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E9%A5%B0%E7%AC%A6"><span class="toc-text">修饰符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%AC%E5%BC%8F%E8%A7%84%E5%88%99"><span class="toc-text">公式规则</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%83%E5%AD%97%E7%AC%A6"><span class="toc-text">元字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E9%95%9E"><span class="toc-text">字符镞</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8F%E8%AF%8D"><span class="toc-text">量词</span></a></li></ol></li></ol>
  </div>
</aside>

  
  
  
</div>

    </main>
    
    
<div class="site-footer-wrapper">
<!--
  <div class="footer-top">
    <a class="left" href="/blog/2020/11/10/%E3%80%90%E5%A5%87%E6%8A%80%E6%B7%AB%E5%B7%A7%E3%80%91%E7%94%B5%E8%84%91%E7%9B%B8%E5%85%B3%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AF%86%E5%90%88%E9%9B%86/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a class="right" href="/blog/2020/11/02/%E3%80%90%E9%9D%A2%E8%AF%95%E9%9B%86%E9%94%A6%E3%80%91%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98%E7%9A%84%E6%94%B6%E9%9B%86%E5%92%8C%E6%80%BB%E7%BB%93(Es6%20%E7%AF%87)/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
-->
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">网站推荐</h5>
          
            <span class="site-footer-item">
              <a href="https://tympanus.net/codrops/" target="_blank">Codrops</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://taobaofed.org/" target="_blank">淘宝前端团队(FED)</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">文档教程</h5>
          
            <span class="site-footer-item">
              <a href="https://es6.ruanyifeng.com/" target="_blank">ES6入门</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://developer.mozilla.org/zh-CN/docs/Web" target="_blank">MDN Web文档</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">兴趣前沿</h5>
          
            <span class="site-footer-item">
              <a href="https://threejs.org/" target="_blank">threejs</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://github.com/justadudewhohacks/face-api.js" target="_blank">face-api</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: xianbin.me@qq.com
      </div>
    
<!--     <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div> -->
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">

  <div class="bottom-bar-left">
<!--
    <a href="/blog/2020/11/10/%E3%80%90%E5%A5%87%E6%8A%80%E6%B7%AB%E5%B7%A7%E3%80%91%E7%94%B5%E8%84%91%E7%9B%B8%E5%85%B3%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AF%86%E5%90%88%E9%9B%86/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/2020/11/02/%E3%80%90%E9%9D%A2%E8%AF%95%E9%9B%86%E9%94%A6%E3%80%91%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98%E7%9A%84%E6%94%B6%E9%9B%86%E5%92%8C%E6%80%BB%E7%BB%93(Es6%20%E7%AF%87)/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
-->
  </div>

  
  
  
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
</div>
    





    
  </body>
</html>